package com.example.mqttdevicesimulator;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

/**
 * Adapter for list in main activity.
 */
public class MqttDeviceAdapter extends RecyclerView.Adapter<MqttDeviceAdapter.ViewHolder> {

    private List<MqttDevice> mqttDeviceList;

    static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView device_image;
        TextView device_name;

        public ViewHolder(View view) {
            super(view);
            device_image = view.findViewById(R.id.mqtt_device_image);
            device_name = view.findViewById(R.id.mqtt_device_name);
        }
    }

    public MqttDeviceAdapter(List<MqttDevice> list) {
        mqttDeviceList = list;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.mqtt_device_item,
                parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        MqttDevice device = mqttDeviceList.get(position);
        holder.device_image.setImageResource(device.getImageId());
        holder.device_name.setText(device.getName());
    }

    @Override
    public int getItemCount() {
        return mqttDeviceList.size();
    }

}
